package com.lwlean.test.aliyun.oss;

import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 * Created by lean on 2016/12/20.
 */
public class OSSUploadService extends RecursiveTask<Boolean> {

    private static final Logger log = LoggerFactory.getLogger(OSSUploadService.class);

    private static List<String> listUrls = new ArrayList<>();

    private String url;

    private OSS oss;

    public OSSUploadService(String url,OSS oss) {
        this.url = url;
        this.oss = oss;
    }

    @Override
    public Boolean compute() {
        File file = new File(url);
        if(!file.isDirectory()){
            try {
                InputStream inputStream = new FileInputStream(file);
                final String fileName = file.getName();
                final long fileSize = file.length();
                final String sidByUrl = getSidByUrl(url);
                final String diskName = "data/oss/"+sidByUrl+"/";
                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setContentLength(inputStream.available());
                metadata.setCacheControl("no-cache");
                metadata.setHeader("Pragma","no-cache");
                metadata.setContentEncoding("UTF-8");
                metadata.setContentType("application/octst-stream");
                metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte.");
                final PutObjectResult putObjectResult = oss.getOssClient().putObject(oss.getBucket().getName(), diskName + fileName, inputStream, metadata);
                final String resultString = putObjectResult.getETag();
                log.info("upload file result: {}",oss.getBucket().getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            final File[] files = file.listFiles();
            for(int i=0;i<files.length;i++){
                final String path = files[i].getPath();
                OSSUploadService task = new OSSUploadService(path,oss);
                task.fork();
                task.join();
            }
        }
        return true;
    }

    public String getSidByUrl(String url){
        final String[] split = url.split("\\\\");
        return split[split.length-2];
    }

    /**
     * 测试oss上传
     */
    public static void ossUploadObject() {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        final String url = "C:\\Users\\lean\\Desktop\\sharedfs\\record\\" + sdf.format(new Date()) + "\\";
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        OSSConfiguration ossConfiguration = new OSSConfiguration();
        OSSUploadService upload = new OSSUploadService(url,ossConfiguration.getOSS());
        final ForkJoinTask<Boolean> submit = forkJoinPool.submit(upload);
        try {
            final Boolean inte = submit.get();
            log.info("get result is :{}", inte);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
